import Data.List

collatz :: Integer -> Integer
collatz n
     | odd n = 3*n + 1
     | otherwise = n `div` 2

collatzSequence = terminate . iterate collatz
                  where terminate (1:_) = [1]
                        terminate (x:xs) = x:terminate xs

findLongest :: Integer -> [Integer] -> [Integer]
findLongest 1 longest = longest
findLongest underN longest
            | lseq > length longest = (findLongest (underN -1) cseq)
            | otherwise = findLongest (underN - 1) longest
              where cseq = collatzSequence underN
                    lseq = length cseq

chains = [collatzSequence num | num <- [900000,899999..899000]]

biggest :: Integer -> [Integer] -> [Integer] -> [Integer]
biggest 999000 used biggie = biggie
biggest underN used biggie
        | underN `elem` used = biggest (underN -1) used biggie
        | lseq > length biggie = biggest (underN -1) newUsed cseq
        | otherwise = biggest (underN -1) newUsed biggie
          where cseq = collatzSequence underN
                lseq = length cseq
                newUsed = union used newUsed

main = print $ biggest 1000000 [] []
--longest [] info = info
--longest ((lgth,num):xs) (maxLgth, num2)
--        | lgth > maxLgth = longest xs (maxLgth, num2)
--        | otherwise = longest xs (maxLgth, num2)
